<template>
	<div>
		<!-- <el-card class="!border-none" shadow="never">
			<el-alert type="warning" title="温馨提示：请勿随意删除" :closable="false" show-icon />
		</el-card> -->
		<el-card class="!border-none" v-loading="pager.loading" shadow="never">
			<!-- <el-button v-perms="['decorate.decorate_hint/add']" type="primary" @click="handleAdd(4)">
				新增弹窗
			</el-button>
			<el-button v-perms="['decorate.decorate_hint/add']" type="primary" @click="handleAdd(5)">
				新增系统消息
			</el-button>
			<el-button v-perms="['decorate.decorate_hint/add']" type="primary" @click="handleAdd(6)">
				新增任务消息
			</el-button>
			<el-button v-perms="['decorate.decorate_hint/add']" type="primary" @click="handleAdd(7)">
				新增站内信
			</el-button> -->
			<!-- <el-button v-perms="['decorate.decorate_hint/add']" type="primary" @click="handleAdd(0)">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button> -->
			<!-- <el-button
                v-perms="['decorate.decorate_hint/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData)"
            >
                删除
            </el-button> -->
			<div class="mt-4">
				<el-table :data="pager.lists" @selection-change="handleSelectionChange">
					<!-- <el-table-column type="selection" width="55" /> -->
					<el-table-column label="标题" show-overflow-tooltip >
						<template #default="{ row }">
							<div  v-if="row.cid==5||row.cid==6||row.cid==7">
								{{row.text}}
							</div>
							<div  v-if="!(row.cid==5||row.cid==6||row.cid==7)">
								{{row.title}}
							</div>
						</template>
					</el-table-column>
					<!-- <el-table-column label="类型">
						<template #default="{ row }">
							<div v-if="row.cid==4||row.cid==5||row.cid==6||row.cid==7">
								<dict-value :options="dictData.hint_type" :value="row.cid" />
							</div>
						</template>
					</el-table-column>
					<el-table-column label="显示状态" prop="is_show">
						<template #default="{ row }">
							<dict-value :options="dictData.show_status" :value="row.is_show" />
						</template>
					</el-table-column>
					<el-table-column label="排序" prop="sort" show-overflow-tooltip />
					<el-table-column label="发布时间">
						<template #default="{ row }">
							<div  v-if="row.cid==4||row.cid==5||row.cid==6||row.cid==7">
								{{row.create_time}}
							</div>
						</template>
					</el-table-column> -->
					<el-table-column label="操作" width="120" fixed="right">
						<template #default="{ row }">
							<el-button v-perms="['decorate.decorate_hint/edit']" type="primary" link
								@click="handleEdit(row)">
								编辑
							</el-button>
							<el-button
                                v-perms="['decorate.decorate_hint/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
								v-if="deleteArr.includes(row.cid)"
                            >
                                删除
                            </el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="flex mt-4 justify-end">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
		<edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
	</div>
</template>

<script lang="ts" setup name="decorateHintLists">
	import { usePaging } from '@/hooks/usePaging'
	import { useDictData } from '@/hooks/useDictOptions'
	import { apiDecorateHintLists, apiDecorateHintDelete } from '@/api/decoration'
	import { timeFormat } from '@/utils/util'
	import feedback from '@/utils/feedback'
	import EditPopup from './edit.vue'

	const editRef = shallowRef<InstanceType<typeof EditPopup>>()
	// 是否显示编辑框
	const showEdit = ref(false)
	
	const deleteArr = ref([4,5,6,7])

	// 查询条件
	const queryParams = reactive({

	})

	// 选中数据
	const selectData = ref<any[]>([])

	// 表格选择后回调事件
	const handleSelectionChange = (val : any[]) => {
		selectData.value = val.map(({ id }) => id)
	}

	// 获取字典数据
	const { dictData } = useDictData('show_status,hint_type')

	// 分页相关
	const { pager, getLists, resetParams, resetPage } = usePaging({
		fetchFun: apiDecorateHintLists,
		params: queryParams
	})

	// 添加
	const handleAdd = async (cid : number) => {
		console.log(cid)
		showEdit.value = true
		await nextTick()
		editRef.value?.open('add')
		editRef.value?.setCid(cid)
	}

	// 编辑
	const handleEdit = async (data : any) => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.setFormData(data)
	}

	// 删除
	const handleDelete = async (id : number | any[]) => {
		await feedback.confirm('确定要删除？')
		await apiDecorateHintDelete({ id })
		getLists()
	}

	getLists()
</script>